package com.hs.app.action.stat;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hs.app.service.IDataService;
import com.hs.chd.action.search.SearchUtil;
import com.hs.core.action.BaseAction;
import com.hs.core.db.DBMgr;
import com.hs.core.db.QueryPageMysql;
import com.hs.core.utils.BigNumCal;
@Controller   
public class StatController  extends BaseAction {
    private static final Logger logger = Logger.getLogger(StatController.class);
	private IDataService dataService;
	@Autowired  
	public void setDataService(IDataService dataService) {
		this.dataService = dataService;
	}
	/**
	  * 按职称分级统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/cftstat.do")   
	 public ModelAndView cftstat(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    String yearstr = request.getParameter("yearstr");
			Map paramMap = new HashMap();
	        DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
	        String sql = "select * from (select (select cvalue from ngeanconstant where ckey=a.zhichengfj and fname='zhichengfj') as zhichengfjvalue,a.zhichengfj,count(*) as cnum from yishengqk a,cfcd_index b where a.wenxianqkid = b.paperid ";
	         if(StringUtils.isNotEmpty(yearstr)){
	         	sql += " and nian="+yearstr+" group by zhichengfj) a order by zhichengfj desc";
	         	paramMap.put("yearstr", yearstr);
	         }else{
	         	sql +=" group by zhichengfj  ) d order by zhichengfj desc";
	         }
	        int totalcount = dbo.queryCountForInt("select sum(cnum) from ("+sql+") g", null);
	        List datalist = dbo.queryList(sql, null);
	        modelMap.put("totalcount", totalcount+"");
		    modelMap.put("datalist", datalist); 
		    modelMap.put("yearstr", yearstr);
			return new ModelAndView("app/stat/cftstat",modelMap); 
			 
     } 
	 
	 /**
	  * 疾病top10统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/disstatimg.do")   
	 public ModelAndView disstatimg(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    List yearlist = SearchUtil.getRecentyears(10);
		    StringBuffer yearbuffer = new StringBuffer();

		    for(int k=0;k<yearlist.size();k++){
        		String years  = (String)yearlist.get(k); 
        		yearbuffer.append("'").append(years).append("'");
        		if( k < yearlist.size()-1){
        			yearbuffer.append(",");
        		}
		    }
			Map datamap = new HashMap();
			DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
	        String sql = "select * from (select danweiymc as herbname,count(1) as cnum from zhongyaozc  group by danweiymc) c order by cnum desc limit 1,10";
	        List datalist = dbo.queryList(sql, null);
	        String subsql =" select nian,count(*) as cnum from cfcd_index where id in(select binganxxid from zhenci where id in(select zhenciid from zhongyaozl where id in(SELECT zhongyaozlid from zhongyaozc where danweiymc=?))) and nian>=2000 group by nian ";
	        for(int i=0;i<datalist.size();i++){
	        	Map data = (Map)datalist.get(i);
	        	String herbname = (String)data.get("herbname");
	        	List singledatalist = dbo.queryList(subsql, new Object[]{herbname});
 	        	StringBuffer databuffer = new StringBuffer();
	        	Map herbmapdata = dataprocess(singledatalist);
	        	for(int k=0;k<yearlist.size();k++){
	        		String years  = (String)yearlist.get(k);  
	        		Map singleherb = (Map)herbmapdata.get(years);
	        		String countnum = "0";
	        		if(singleherb != null){
	        			countnum = singleherb.get("cnum")+"";
	        		}
	        		databuffer.append(countnum).append(","); 
	        	} 
	        	datamap.put(herbname, databuffer.toString());
	        }
	        modelMap.put("yearbuffer", yearbuffer.toString()); 
	        modelMap.put("datamap", datamap); 
			return new ModelAndView("app/stat/dwystat",modelMap);
			 
     } 
	 
	 /**
	  * 单味药统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/herbstat.do")   
	 public ModelAndView herbstat(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    List yearlist = SearchUtil.getRecentyears(10);
		    StringBuffer yearbuffer = new StringBuffer();

		    for(int k=0;k<yearlist.size();k++){
        		String years  = (String)yearlist.get(k); 
        		yearbuffer.append("'").append(years).append("'");
        		if( k < yearlist.size()-1){
        			yearbuffer.append(",");
        		}
		    }
			Map datamap = new HashMap();
			DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
	        String sql = "select * from (select danweiymc as herbname,count(1) as cnum from zhongyaozc  group by danweiymc) c order by cnum desc limit 1,10";
	        List datalist = dbo.queryList(sql, null);
	        String subsql =" select nian,count(*) as cnum from cfcd_index where id in(select binganxxid from zhenci where id in(select zhenciid from zhongyaozl where id in(SELECT zhongyaozlid from zhongyaozc where danweiymc=?))) and nian>=2000 group by nian ";
	        for(int i=0;i<datalist.size();i++){
	        	Map data = (Map)datalist.get(i);
	        	String herbname = (String)data.get("herbname");
	        	List singledatalist = dbo.queryList(subsql, new Object[]{herbname});
 	        	StringBuffer databuffer = new StringBuffer();
	        	Map herbmapdata = dataprocess(singledatalist);
	        	for(int k=0;k<yearlist.size();k++){
	        		String years  = (String)yearlist.get(k);  
	        		Map singleherb = (Map)herbmapdata.get(years);
	        		String countnum = "0";
	        		if(singleherb != null){
	        			countnum = singleherb.get("cnum")+"";
	        		}
	        		databuffer.append(countnum).append(","); 
	        	} 
	        	datamap.put(herbname, databuffer.toString());
	        }
	        modelMap.put("yearbuffer", yearbuffer.toString()); 
	        modelMap.put("datamap", datamap); 
			return new ModelAndView("app/stat/dwystat",modelMap);
			 
     } 
	 
	 private Map dataprocess(List datalist){
		 Map mapdata = new HashMap();
		 for(int i = 0;i<datalist.size();i++){
			 Map map = (Map)datalist.get(i);
			 String year = map.get("nian")+"";
			 mapdata.put(year, map);
		 }
		 
		 return mapdata;
	 }
	 /**
	  * 按行医年数统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/yopstat.do")   
	 public ModelAndView yopstat(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
			String yearstr = request.getParameter("yearstr");
			Map paramMap = new HashMap();
	        DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
	        String sql = "select * from (select (select cvalue from ngeanconstant where ckey=a.xingyins and fname='xingyins') as xingyinsvalue,a.xingyins,count(*) as cnum from yishengqk a,cfcd_index b where a.wenxianqkid = b.paperid ";
	         if(StringUtils.isNotEmpty(yearstr)){
	         	sql += " and nian="+yearstr+" group by xingyins) a order by xingyins desc";
	         	paramMap.put("yearstr", yearstr);
	         }else{
	         	sql +=" group by xingyins  ) d order by xingyins desc";
	         }
	        int totalcount = dbo.queryCountForInt("select sum(cnum) from ("+sql+") g", null);
	        List datalist = dbo.queryList(sql, null);
	        modelMap.put("totalcount", totalcount+"");
		    modelMap.put("datalist", datalist); 
		    modelMap.put("yearstr", yearstr);
			return new ModelAndView("app/stat/yopstat",modelMap);
			 
     } 
	 /**
	  * 按单味药统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/shstat.do")   
	 public ModelAndView shstat(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String yearstr = request.getParameter("yearstr");
		 Map paramMap = new HashMap();
         DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
         String sql = "select * from (select danweiymc as herbname,count(*) as cnum from zhongyaozc  group by danweiymc) a order by cnum desc ";
         
         int totalcount = dbo.queryCountForInt("select sum(cnum) from ("+sql+") g", null);
         QueryPageMysql qp = new QueryPageMysql(request,request.getContextPath()+"/stat/shstat.do",sql,paramMap); 
     	//	     
		     qp.query();  
		     String pagenum =  qp.getCurrentPageNo() + "";
		     String pagecount = qp.getPerPageCount() + "";
		     modelMap.put("pagenum", pagenum);
		     modelMap.put("pagecount", pagecount);
		     modelMap.put("pagestr", qp.getPageStr());
		     modelMap.put("totalcount", totalcount+"");
		     modelMap.put("datalist", qp.getDataList()); 
		     modelMap.put("yearstr", yearstr);
            
			return new ModelAndView("app/stat/shstat",modelMap);
			 
     } 
	 /**
	  * 按穴位统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/pointstat.do")   
	 public ModelAndView pointstat(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		    String yearstr = request.getParameter("yearstr");
		    Map paramMap = new HashMap();
            DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
            String sql = "select * from (select xuewei,count(*) as cnum from zhenjiuxw where zhenjiuzlid in(select id from zhenjiuzl where zhenciid in(select id from zhenci where binganxxid in(select id from cfcd_index ";
            if(StringUtils.isNotEmpty(yearstr)){
            	sql += " where nian="+yearstr+"))) group by xuewei) a order by cnum desc";
            	paramMap.put("yearstr", yearstr);
            }else{
            	sql +="))) group by xuewei) a order by cnum desc";
            }
            int totalcount = dbo.queryCountForInt("select sum(cnum) from ("+sql+") g", null);
            QueryPageMysql qp = new QueryPageMysql(request,request.getContextPath()+"/stat/pointstat.do",sql,paramMap); 
        	//	     
		     qp.query();  
		     String pagenum =  qp.getCurrentPageNo() + "";
		     String pagecount = qp.getPerPageCount() + "";
		     modelMap.put("pagenum", pagenum);
		     modelMap.put("pagecount", pagecount);
		     modelMap.put("pagestr", qp.getPageStr());
		     modelMap.put("totalcount", totalcount+"");
		     modelMap.put("datalist", qp.getDataList()); 
		     modelMap.put("yearstr", yearstr);
		     
			return new ModelAndView("app/stat/pointstat",modelMap); 
     } 
	 /**
	  * 按给药方式统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/useagestat.do")   
	 public ModelAndView useagestat(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String yearstr = request.getParameter("yearstr");
			Map paramMap = new HashMap();
	        DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
	        String sql = "select * from (select geiyaofs,count(*) as cnum from zhongyaozl where zhenciid IN(select id from zhenci where binganxxid in(select id from cfcd_index  ";
	         if(StringUtils.isNotEmpty(yearstr)){
	         	sql += " where nian="+yearstr+" )) group by geiyaofs) a order by cnum desc";
	         	paramMap.put("yearstr", yearstr);
	         }else{
	         	sql +=" )) group by geiyaofs  ) d order by cnum desc";
	         }
	        int totalcount = dbo.queryCountForInt("select sum(cnum) from ("+sql+") g", null);
	        List datalist = dbo.queryList(sql, null);
	        modelMap.put("totalcount", totalcount+"");
		    modelMap.put("datalist", datalist); 
		    modelMap.put("yearstr", yearstr);
			return new ModelAndView("app/stat/useagestat",modelMap);
			 
     } 
	 /**
	  * 按疾病统计
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/stat/disstat.do")   
	 public ModelAndView disstat(HttpServletRequest request,
					HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String yearstr = request.getParameter("yearstr");
		    Map paramMap = new HashMap();
         DBMgr dbo = DBMgr.getDbUtilInst(request.getSession().getServletContext());
         String sql = "select * from (select disname,count(*) as cnum from cfcd_disnames a,cfcd_index b  where a.binganxxid=b.id ";
         if(StringUtils.isNotEmpty(yearstr)){
         	sql += " where b.nian="+yearstr+" group by disname a order by cnum desc";
         	paramMap.put("yearstr", yearstr);
         }else{
         	sql +=" group by disname) a order by cnum desc";
         }
         int totalcount = dbo.queryCountForInt("select sum(cnum) from ("+sql+") g", null);
         QueryPageMysql qp = new QueryPageMysql(request,request.getContextPath()+"/stat/disstat.do",sql,paramMap); 
     	//	     
		     qp.query();  
		     String pagenum =  qp.getCurrentPageNo() + "";
		     String pagecount = qp.getPerPageCount() + "";
		     modelMap.put("pagenum", pagenum);
		     modelMap.put("pagecount", pagecount);
		     modelMap.put("pagestr", qp.getPageStr());
		     modelMap.put("totalcount", totalcount+"");
		     modelMap.put("datalist", qp.getDataList()); 
		     modelMap.put("yearstr", yearstr);
			return new ModelAndView("app/stat/disstat",modelMap);
			 
     } 
	
}
